
#include <cassert>
//#include "ApConstant.h"
#include "ArrayListMsg.h"

ArrayListMsg::ArrayListMsg()
{
	m_nLevel = 0;
}

ArrayListMsg::~ArrayListMsg()
{
	m_sStrValues.clear();
	m_aListValues.clear();
	m_vecPosInfo.clear();
}
	
ArrayListMsg::ArrayListMsg(const ArrayListMsg& oCopy)
{
	m_sStrValues = oCopy.m_sStrValues;
	m_aListValues = oCopy.m_aListValues;
	m_sStrValue =oCopy.m_sStrValue;
	m_nLevel = oCopy.m_nLevel;
	m_vecPosInfo = oCopy.m_vecPosInfo;
}

//----------------------------------------------------------------
// 根据字符串进行初始化解析的构造函数
// 输入参数： pStrMsg  需要进行解析的字符串指针
ArrayListMsg::ArrayListMsg(const QString& sStrMsg,int nLevel)
{
	m_nLevel = nLevel;
	Parse(sStrMsg,nLevel);
}

//=操作符重载
void ArrayListMsg::operator=(const ArrayListMsg& oCopy)
{
	m_sStrValues = oCopy.m_sStrValues;
	m_aListValues = oCopy.m_aListValues;
	m_sStrValue =oCopy.m_sStrValue;
	m_nLevel = oCopy.m_nLevel;
	m_vecPosInfo = oCopy.m_vecPosInfo;
}

//清除数据元素
void ArrayListMsg::clear()
{
	m_sStrValues.clear();
	m_aListValues.clear();
	m_vecPosInfo.clear();
}

//----------------------------------------------------------------
//函数功能：解析字符串
//输入参数： pStrMsg  需要进行解析的字符串指针
//返 回 值： 0：成功，其它失败
int ArrayListMsg::Parse(const QString& sStrMsg,int nLevel)
{
	//assert
    if (sStrMsg.trimmed().size() == 0)
		return 0;

    QStringList vRec = strutils::explode(ApConst::SEPARATOR_RECORD[nLevel], sStrMsg);
    int nSize = vRec.size();
    for (int i = 0; i < nSize; i++)
	{
        int nRtn = -1;
//        qDebug()<<"parse "<<i<<ApConst::SEPARATOR_RECORD[nLevel]<<":"<<vRec.at(i);
        if (nLevel < ApConst::SEPARATOR_MAX_LEVEL - 1)
		{
            nRtn = vRec.at(i).indexOf(ApConst::SEPARATOR_RECORD[nLevel+1]);
            if (nRtn != -1)
			{
				ArrayListMsg aList;
                if (aList.Parse(vRec[i],nLevel+1) > 0)
                {
                    POS_INFO stInfo;
                    stInfo.eType = E_ARRAYLIST;
                    stInfo.nPos = m_aListValues.size();
                    m_vecPosInfo.push_back(stInfo);

                    m_aListValues.push_back(aList);
                }
            }
			else
			{
				POS_INFO stInfo;
                stInfo.eType = E_STRING;
				stInfo.nPos = m_sStrValues.size();
				m_vecPosInfo.push_back(stInfo);

				m_sStrValues.push_back(vRec[i]);
			}
		}
		else
		{
			POS_INFO stInfo;
            stInfo.eType = E_STRING;
			stInfo.nPos = m_sStrValues.size();
			m_vecPosInfo.push_back(stInfo);

			m_sStrValues.push_back(vRec[i]);
		}
	}

//    qDebug()<<"parse size="<<m_vecPosInfo.size();
    return m_sStrValues.size()+m_aListValues.size();
}

//----------------------------------------------------------------
//函数功能：获得当前集合中的元素个数
//输入参数：无
//返 回 值：集合中的元素个数
int ArrayListMsg::size() const
{
	return m_vecPosInfo.size();
}

//容器容量
//int ArrayListMsg::Capacity() const
//{
//	if ( m_sStrValues.size() > 0 )
//		return m_sStrValues.capacity();
//	else
//		return m_aListValues.capacity();
//}


//容器容量调整
void ArrayListMsg::Reserve(size_t nSize)
{
	if ( m_sStrValues.size() > 0 )
		m_sStrValues.reserve(nSize);
	else
		m_aListValues.reserve(nSize);
}

//----------------------------------------------------------------
//函数功能：将对象转换为字符串
//输入参数：用于接收的字符串指针
//返 回 值：无
void ArrayListMsg::ToQString(QString& sOutMsg) const
{
    ToQString(sOutMsg,m_nLevel);
}

QString ArrayListMsg::ToQString() const
{
    QString sVal("");
    ToQString(sVal,m_nLevel);
	return sVal;
}

void ArrayListMsg::ToQString(QString& sOutMsg,int nIndex ) const
{
    QString sBuff("");
	size_t nSize = m_vecPosInfo.size();
	for (size_t i = 0; i < nSize; i++)
	{
        if ( m_vecPosInfo[i].eType == E_STRING )
		{
            sBuff += strutils::variantToQString(m_sStrValues[m_vecPosInfo[i].nPos]);
            sBuff += ApConst::SEPARATOR_RECORD[nIndex];
		}
		else
		{
            m_aListValues[m_vecPosInfo[i].nPos].ToQString(sBuff, nIndex + 1);
            sBuff += ApConst::SEPARATOR_RECORD[nIndex];
		}
	}
	sOutMsg+=sBuff;
	return;
}

void ArrayListMsg::AddValue(ArrayListMsg& aRec)
{
	POS_INFO stInfo;
	stInfo.eType = E_ARRAYLIST;
	stInfo.nPos = m_aListValues.size();
	m_vecPosInfo.push_back(stInfo);

	m_aListValues.push_back(aRec);
}

ArrayListMsg ArrayListMsg::GetValue(size_t nIndex) const
{
	assert(nIndex>=0 && nIndex < m_vecPosInfo.size());
	assert(m_vecPosInfo[nIndex].eType == E_ARRAYLIST && m_vecPosInfo[nIndex].nPos < m_aListValues.size());

	return m_aListValues[m_vecPosInfo[nIndex].nPos];
}

void ArrayListMsg::GetValue(size_t nIndex,ArrayListMsg& aRec) const
{
	assert(nIndex>=0 && nIndex < m_aListValues.size());
	aRec = m_aListValues[nIndex];
}

void ArrayListMsg::AddValue(size_t nIndex,ArrayListMsg& aRec)
{
	assert(nIndex>=0 && nIndex < m_vecPosInfo.size());
	assert(m_vecPosInfo[nIndex].eType == E_ARRAYLIST && m_vecPosInfo[nIndex].nPos < m_aListValues.size());

	m_aListValues[m_vecPosInfo[nIndex].nPos].AddValue(aRec);
}

ArrayListMsg ArrayListMsg::GetValue(size_t nRecIndex,size_t nEleIndex) const
{
	assert(nRecIndex>=0 && nRecIndex < m_vecPosInfo.size());
	assert(m_vecPosInfo[nRecIndex].eType == E_ARRAYLIST && m_vecPosInfo[nRecIndex].nPos < m_aListValues.size());

    return m_aListValues[m_vecPosInfo[nRecIndex].nPos].GetValue(nEleIndex);
}

//int ArrayListMsg::ColumnSize()
//{
//    return qMax(m_sStrValues.size(), m_aListValues.size());
//}
